﻿//将查询结果集封装为json对象
var mysql = require('mysql');
var pool = mysql.createPool({
	user : 'root',
	password : '',
	database : 'book'
});

pool.getConnection(function(err, connection){
	if(err){
		console.log(err.message);
		connection.release();
		return;
	}
	connection.query('select * from author_ limit 2', function(err, rows, fields){
		if(err){
			console.log(err.message);
			connection.release();
			return;
		}
		var json;
		if(rows && fields){
			json = [];
			for(var i = 0; i < rows.length; i ++){
				json[i] = {};
				for(var j = 0; j < fields.length; j ++){
					json[i][fields[j].name] = rows[i][fields[j].name];//fields[j].name表示列名
				}
			}
		}
		console.log(json);
		connection.release();//回收connection
		pool.end();
	});
	
});